/**
 * Copyright (C) 2014 Luvina Software Company
 * MstGroupDaoImpl.java, Dec 17, 2014, Nguyen Huu Luan LA11_AM
 *
 */
package net.luvina.manageuser.dao.impl;

import java.util.ArrayList;
import java.util.List;

import net.luvina.manageuser.dao.MstGroupDao;
import net.luvina.manageuser.entities.MstGroup;

/**
 * Thực thi giao diện MstGroupDao và lấy về danh sách phòng ban có trong DB.
 * 
 * @author luan
 * 
 */
public class MstGroupDaoImpl extends BaseDaoImpl implements MstGroupDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see manageuser.dao.UserDao#getAllGroups()
	 */
	public List<MstGroup> getAllMstGroups() {
		List<MstGroup> lsGroups = new ArrayList<MstGroup>();
		if (connectToDB()) {
			try {
				StringBuilder sqlCommand = new StringBuilder();
				sqlCommand.append("SELECT g.group_id, g.group_name ");
				sqlCommand.append("FROM mst_group AS g ");
				sqlCommand.append(" ORDER BY group_id ASC ");

				preparedStatement = connection.prepareStatement(sqlCommand
						.toString());

				rs = preparedStatement.executeQuery();

				if (rs != null) {
					while (rs.next()) {
						MstGroup group = new MstGroup();
						group.setGroupId(rs.getInt("group_id"));
						group.setGroupName(rs.getString("group_name"));
						lsGroups.add(group);
					}
					rs.close();
				}

			} catch (Exception e) {
				System.out.println("an exception occur: " + e.getMessage());
			}
			closeConnect();
		}
		return lsGroups;
	}

}
